package com.stepshop.wechat.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stepshop.wechat.entity.MsgValueColor;
import com.stepshop.wechat.factory.MsgSendFactory;
import com.stepshop.wechat.msg.MsgSend;
import com.stepshop.wechat.utils.HttpClientUtil;
import com.stepshop.wechat.utils.MsgSendUtil;

@Controller
@RequestMapping("/sendMsg/")
public class SendMsgController {
	private HttpClientUtil httpClientUtil=new HttpClientUtil();
	@RequestMapping("feeMsg")
	@ResponseBody
	public void sendFeeMsg(HttpServletRequest request,HttpServletResponse response){
		Map<String,MsgValueColor> dataMap=new HashMap<String,MsgValueColor>();
		dataMap.put("orderMoneySum", new MsgValueColor("5288","#ff0000"));
		dataMap.put("first", new MsgValueColor("您已经支付成功"));
		dataMap.put("orderProductName", new MsgValueColor("神州八号"));
		dataMap.put("Remark", new MsgValueColor("订单已经进库,我们会及时安排发货"));
		String url="http://www.baidu.com";
		String temid="i2_e1jfRyP1wbcrR-EarOCCKul6c66-acVd-JAVOl04";
		String openid="o3QjlwGBjeKHvc16IR-sMO8KXvec";
		MsgSend msgSend = MsgSendFactory.getMsgSend("com.stepshop.wechat.msg.ScanMsgSend");
		MsgSendUtil.send(dataMap, url, temid, openid);
	}
	@RequestMapping("getTemplete")
	public void getTemplete(HttpServletRequest request,HttpServletResponse response){
		InputStream input = httpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=UA9H5ociWsirSKzzI2Gom12v5FvhsdpfqbWJpFu9rbTH0TOPulmA6A8Q4kjSNOlg8HsF6QHndPhALP6kvevtK60VkCVlZRdTCPKPuBuhH_J16GiPdZ_DeD4P6GFjRbXINEVcAEAFWN","UTF-8");
		String json = httpClientUtil.InputStream2Url(input);
		JSONObject jsonObj = JSON.parseObject(json);
		JSONObject nowObj=new JSONObject();
		List objs = (List) jsonObj.get("template_list");
		Iterator iterator = objs.iterator();
		while(iterator.hasNext()){
			JSONObject obj = (JSONObject) iterator.next();
			Object template_id = obj.get("template_id");
			if("SRhyPeusD84sjARrBB0Ppbjp0mC7FmyYrIbWM4r1fRE".equals(template_id)){
				nowObj=obj;
				break;
			}
		}
//		System.out.println(nowObj);
		String oldContent=(String) nowObj.get("content");
		String content=oldContent.replaceAll("\\s","");
//		System.out.println(content);
//		String regex = "(?<=\\{\\{).*(?=\\}\\})";
//        Pattern p = Pattern.compile(regex);
//        Matcher m = p.matcher(content);
//        while (m.find()) {
//            System.out.println(m.group());
//        }
		System.out.println(content);
		String[] split = content.split("}}");
		List<String> sps=new ArrayList<String>();
		for(String sp:split){
			System.out.println(sp.substring(sp.indexOf("{{")+2,sp.indexOf(".DATA")));
			sps.add(sp.substring(sp.indexOf("{{")+2,sp.indexOf(".DATA")));
		}
	}
}	
